package top.houry.netty.barrage.attr;

import java.util.concurrent.atomic.AtomicLong;

public class BarrageLimitAttrInfo {
    private AtomicLong counter = new AtomicLong(0);
    private static long timestamp = System.currentTimeMillis();
    private long limit;

    /**
     *  毫秒
     */
    private long time;
    public BarrageLimitAttrInfo(long limit, long time) {
        this.limit = limit;
        this.time = time;
    }


    public void inc() {
        counter.incrementAndGet();
    }

    public boolean tryAcquire() {
        long now = System.currentTimeMillis();
        if (now - timestamp < time) {
            return counter.get() < limit;
        } else {
            counter = new AtomicLong(0);
            timestamp = now;
            return false;
        }
    }

}
